﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using Tesla.Core;

namespace Tesla.Input {
    /// <summary>
    /// Mouse static class that polls the current mouse state. Usage is equivalent
    /// to directly using an IMouseWrapper.
    /// </summary>
    public static class Mouse {
        private static IMouseWrapper _mouseWrapper;

        /// <summary>
        /// Set the default window handle for the mouse. This is necessary to get the client coordinates 
        /// for the mouse.
        /// </summary>
        public static IntPtr Handle {
            get {
                CheckAndCreate();
                return _mouseWrapper.Handle;
            }
            set {
                CheckAndCreate();
                _mouseWrapper.Handle = value;
            }
        }

        static Mouse() {
            Engine.Services.ServiceAdded += new EventHandler(ProviderChanged);
            Engine.Services.ServiceChanged += new EventHandler(ProviderChanged);
            Engine.Services.ServiceRemoved += new EventHandler(ProviderRemoved);
            CheckAndCreate();
            System.Diagnostics.Debug.WriteLine("In static mouse constructor.");
        }

        /// <summary>
        /// Get the current mouse state.
        /// </summary>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if an IMouseProvider is not a registered service.</exception>
        /// <returns>MouseState struct</returns>
        public static MouseState GetMouseState() {
            CheckAndCreate();
            return _mouseWrapper.GetMouseState();
        }

        /// <summary>
        /// Get the current mouse state of a specified window.
        /// </summary>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if an IMouseProvider is not a registered service.</exception>
        /// <param name="handle">Handle to the window</param>
        /// <returns>MouseState struct</returns>
        public static MouseState GetMouseState(IntPtr handle) {
            CheckAndCreate();
            return _mouseWrapper.GetMouseState(handle);
        }

        /// <summary>
        /// Sets the position of the mouse cursor in the client window.
        /// </summary>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if an IMouseProvider is not a registered service.</exception>
        /// <param name="x">X coordinate</param>
        /// <param name="y">Y coordinate</param>
        public static void SetPosition(int x, int y) {
            CheckAndCreate();
            _mouseWrapper.SetPosition(x, y);
        }

        private static void ProviderChanged(Object service, EventArgs args) {
            //If a service that has been changed (new service) and is a mouse provider, respond.
            if(service != null && service is IMouseProvider) {
                IMouseProvider provider = service as IMouseProvider;
                if(provider == null) {
                    throw new TeslaException("No mouse input provider is set");
                }
                _mouseWrapper = provider.MouseWrapper;
                System.Diagnostics.Debug.WriteLine("Mouse provider changed.");
            }
        }

        private static void ProviderRemoved(Object service, EventArgs args) {
            //If all services removed, or a mouse provider removed, respond.
            if(service == null || service is IMouseProvider) {
                _mouseWrapper = null;
                System.Diagnostics.Debug.WriteLine("Mouse provider removed.");
            }
        }

        private static void CheckAndCreate() {
            if(_mouseWrapper == null) {
                IMouseProvider provider = Engine.Services.GetService<IMouseProvider>();
                if(provider == null) {
                    throw new TeslaException("No mouse input provider is set");
                }
                _mouseWrapper = provider.MouseWrapper;
            }
        }
    }
}
